import Reflux from 'reflux';
import Actions from './Actions';
import DB from '../../../../app/db';
import update from 'react-addons-update';
import StorageUtils from "../../../../app/storage";
import {generateUUID} from "../Const";
import {Control} from "react-keeper";

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    userInfo: {},
    activeMenu: 'all',
    subject: {
      // 全部的
      all: [],
      my: {
        // 我创建的
        create: [],
        // 我参与过的
        voted: []
      }
    }
  },

  customInit() {
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getUser()}});
    this.updateComponent();
    this.getAllSubject();
    this.getMyCreate();
    this.getMyVoted();
  },

  initAllCreator() {
    let t = this;
    let subjects = t.data.subject.all;
    subjects.map(subject=>{
      DB.WechatAPI.getProprietorById({id: subject.creatorId}).then(
        function (content) {
          let proprietor = content;
          if (proprietor && proprietor.id !== null) {
            t.data.subject = update(t.data.subject, {
              all: {$apply: datas => {
                return datas.map(data=>{
                  if (data.id === subject.id) {
                    return update(data, {
                      creatorName: {$set: proprietor.name}
                    });
                  } else {
                    return data;
                  }
                })
              }}
            });
            t.updateComponent();
            DB.WechatAPI.getLocalUser({openId: proprietor.openid}).then(
              function (content) {
                t.data.subject = update(t.data.subject, {
                  all: {$apply: datas => {
                    return datas.map(data=>{
                      if (data.id === subject.id) {
                        return update(data, {
                          creatorHeadImgUrl: {$set: content.headImgUrl}
                        });
                      } else {
                        return data;
                      }
                    })
                  }}
                });
                t.updateComponent();
              },
              function (error) {
              }
            );
          }
        },
        function (error) {
          console.log(error);
        }
      );
    });
  },

  allSubject() {
    this.data.activeMenu = 'all';
    this.updateComponent();
  },

  mySubject() {
    this.data.activeMenu = 'my';
    this.updateComponent();
  },

  newSubject() {
    Control.go('/home/vote/edit/' + generateUUID());
  },

  getAllSubject() {
    let t = this;
    DB.vote.allSubject().then(
      function (content) {
        t.data.subject.all = content;
        t.updateComponent();
        t.initAllCreator();
      },
      function (error) {
        t.data.subject.all = [];
        t.updateComponent();
        console.log(error);
      }
    );
  },

  // 根据投票创建人的ID查询创建人信息(查询我发布的投票)
  getMyCreate() {
    let t = this;
    DB.vote.allSubjectByCreator({creatorId: StorageUtils.getStorageItem('proprietor').id}).then(
      function (content) {
        t.data.subject.my.create = content;
        t.updateComponent();
      },
      function (error) {
        t.data.subject.my.create = [];
        t.updateComponent();
        console.log(error);
      }
    );
  },

  // 根据投票创建人的ID查询创建人信息(查询我参与的)
  getMyVoted() {
    let t = this;
    DB.vote.allSubjectMyVoted({voterId: StorageUtils.getStorageItem('proprietor').id}).then(
      function (content) {
        t.data.subject.my.voted = content;
        t.updateComponent();
      },
      function (error) {
        t.data.subject.my.voted = [];
        t.updateComponent();
        console.log(error);
      }
    );
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
